<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/page/common/commonConfig.jsp"%>

<html>

<head>
	<base href="<%=basePath%>">
	<title>增加资源</title>
	<script src="<%=basePath%>component/layout.js"></script>
    <script src="<%=basePath%>component/toolbar.js"></script>
	<script type='text/javascript' src='<%=basePath%>dwrsystem/interface/resourceService.js'></script>
	<script type='text/javascript' src='<%=basePath%>dwrsystem/engine.js'></script>
	<script type="text/javascript" src="<%=basePath%>js/validateJS.js"></script>
</head>

<body>
	<div id="dataObj">
	<div id="displayDiv" class="formlayout" style="width:100%;height:95%;overflow:auto;">
		<table id="addTable" >
			<tr>
				<th>资源名称</th>
				<td height="27">
					<input type="hidden" name="tresourcesystem.superResourceId" id="tresourcesystem.superResourceId"/>
					<input name="tresourcesystem.resourceName" id="tresourcesystem.resourceName" maxlength='25'><span class="enterInput">*</span>
				</td>
			</tr>
			<tr>
				<th>资源地址</th>
				<td height="27">
					<input name="tresourcesystem.resourceUrl" id="tresourcesystem.resourceUrl" maxlength='25'><span class="enterInput">*</span>
				</td>
			</tr>
			<tr>
				<th>备注</th>
				<td>
					<textarea rows="3" cols="80" name="tresourcesystem.note" id="tresourcesystem.note"></textarea>
				</td>
			</tr>
		</table>
	</div></div>
</body>
	
<script type="text/javascript">
var nowurl = '${param.nowurl}';
var dhxLayout = createLayoutNormal('1C',false,true);

$(function (){
	configLayout();
});

function configLayout(){
	var a = dhxLayout.cells('a');
	a.hideHeader();
	a.setText('主页面');
	a.fixSize(true, true);
	if(power.getPower() != '' && (power.getPowerInsert() || power.getPowerDelete()))
		createToolbarFromDHXLayoutCell(a,function(toolbar){
		    toolbar.addText("info", 1, "操作:");
		    toolbar.addSeparator("sep1", 2);
		    if(power.getPowerInsert())toolbar.addButton("add", 3, "提交", "save.gif", "save_dis.gif");
		    if(power.getPowerDelete())toolbar.addButton("cancel", 4, "取消", "undo.gif", "undo_dis.gif");
		    toolbar.attachEvent("onClick", function(id) {
		    	if(id == 'add'){
		    		validateAdd();
		    	}
		    	if(id == 'cancel'){
		    		backAdd();
		    	}
		    });
		});
	a.attachObject('dataObj');
}

function validateAdd(){
	var parentResourceId = parent.window.resourceId;
	var rurl = document.getElementById('tresourcesystem.resourceUrl').value;
	if(trim(rurl)==''){parent.parent.parent.notifyWarn('请输入资源地址');return;}
	if (!validatePattern.validateInputZZ(rurl,true)){
		parent.parent.parent.notifyWarn('资源地址非法，输入只能是[汉字,数字、26个英文字母或者下划线]');
		return ;
	}
	var rname = document.getElementById('tresourcesystem.resourceName').value;
	if(trim(rname)==''){parent.parent.parent.notifyWarn('请输入资源名称');return;}
	if (!validatePattern.validateInputZZ(rname,false)){
		parent.parent.parent.notifyWarn('资源名称非法，输入只能是[汉字,数字、26个英文字母或者下划线]');
		return ;
	}
	var rnote = document.getElementById('tresourcesystem.note').value;
	layoutProgressOn(dhxLayout);
	resourceService.hasTResourceName({superResourceId:parentResourceId, resourceName:rname}, function(data){
		if(data == 1){
			parent.parent.parent.notifyWarn('已存在相同资源名称');layoutProgressOff(dhxLayout);
			return;
		}else{
			var mytree = parent.parent.window.MyTree;//标签页中寻找tree
			if(nowurl == 'edit'){ // 增加子资源
				var rlevel = mytree.getNodeLevel(parentResourceId) + 1;
				resourceService.insertTResource({resourceName:rname, resourceUrl:rurl, note:rnote, superResourceId:parentResourceId, resourceLevel:rlevel}, function(rid){
					layoutProgressOff(dhxLayout);
					mytree.addNode(parentResourceId, rid, rname, 0, 0, 0, 0, "SELECT,CALL");
		   		});
			}else if(nowurl == 'list'){ // 增加子资源
				var rlevel = mytree.getNodeLevel(parentResourceId) + 1;
				resourceService.insertTResource({resourceName:rname, resourceUrl:rurl, note:rnote, superResourceId:parentResourceId, resourceLevel:rlevel}, function(rid){
					layoutProgressOff(dhxLayout);
					mytree.addNode(parentResourceId, rid, rname, 0, 0, 0, 0, "");
					document.location.href = '<%=basePath%>page/system/resource/resource_list.jsp?superResourceId=' + parentResourceId;
		   		});
			}else if(nowurl == 'listtop'){ // 增加顶级资源
				resourceService.insertTResource({resourceName:rname, resourceUrl:rurl, note:rnote, superResourceId:'0', resourceLevel:1}, function(rid){
					layoutProgressOff(dhxLayout);
					parent.window.MyTree.addNode(0, rid, rname, 0, 0, 0, 0, "");
					document.location.href = '<%=basePath%>page/system/resource/resource_listtop.jsp?superResourceId=0';
		   		});
			}
		}
	});		
}

function backAdd(){
	var parentResourceId = parent.window.resourceId;
	layoutProgressOn(dhxLayout);
	if(nowurl == 'edit'){
		document.location.href = '<%=basePath%>system-resource/ResourceAction!resourceQueryForEdit.action?resourceId=' + parentResourceId;
	}else if(nowurl == 'list'){
		document.location.href = '<%=basePath%>page/system/resource/resource_list.jsp?superResourceId=' + parentResourceId;
	}else if(nowurl == 'listtop'){
		document.location.href = '<%=basePath%>page/system/resource/resource_listtop.jsp?superResourceId=0';
	}
}

</script>
	
</html>
